package com.acom.bootredis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.acom.bootredis.entity.RedisEntity;
import com.acom.bootredis.service.RedisServiceImpl;
import com.acom.bootredis.util.R;

@RequestMapping("/user")
@RestController
public class RedisUserController {
	  @Autowired
	    private RedisServiceImpl service;
	/**
	 * 添加
	 * @param redisEntity
	 * @return
	 */
	@RequestMapping("/add")
    public R add(RedisEntity redisEntity) {
    	R r = new R();
    	service.put(redisEntity.getRedisKey(), redisEntity, -1);
    	r.setData(redisEntity);
        return r;
    }
	/**
	 * 获取
	 * @param get
	 * @return
	 */
	@RequestMapping("/get")
    public R get(String id) {
    	R r = new R();
    	try {
			r.setData(service.get(id));
		} catch (Exception e) {
			e.printStackTrace();
			r.setCode(e.getClass().getSimpleName());
			r.setMsg(e.getMessage());
		}
        return r;
    }
	/**
	 * 删除
	 * @param del
	 * @return
	 */
	@RequestMapping("/del/{id}")
    public R del(@PathVariable("id") String id) {
    	R r = new R();
    	try {
			service.remove(id);
		} catch (Exception e) {
			e.printStackTrace();
			r.setCode(e.getClass().getSimpleName());
			r.setMsg(e.getMessage());
		}
        return r;
    }
	/**
	 * 更新
	 * @param update
	 * @return
	 */
	@RequestMapping("/update")
    public R update(RedisEntity redisEntity) {
    	R r = new R();
    	try {
			service.get(redisEntity.getRedisKey());
			service.put(redisEntity.getRedisKey(), redisEntity, -1);
			r.setData(redisEntity);
		} catch (Exception e) {
			e.printStackTrace();
			r.setCode(e.getClass().getSimpleName());
			r.setMsg(e.getMessage());
		}
        return r;
    }
	/**
	 * 获取所有
	 * @param getAll
	 * @return
	 */
	@RequestMapping("/getAll")
    public R getAll() {
    	R r = new R();
    	r.setCount(service.getAll().size()+"");
    	r.setData(service.getAll());
        return r;
    }
}
